package com.example.beikeyigou.goods;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beikeyigou.BuyNumDialog;
import com.example.beikeyigou.CartActivity;
import com.example.beikeyigou.R;
import com.example.beikeyigou.login.UserBean;
import com.example.beikeyigou.untils.BaseActivity;
import com.example.beikeyigou.untils.PhotoView;
import com.example.beikeyigou.untils.SaveUserUtils;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsDetailsActivity extends BaseActivity {

    private DetailsBean.DataBean dataBean;
    private TextView titleTv,desTv,countTv,priceTv;
    private ImageView backIv;
    private Button addBtn,buyBtn;
    private GridView likegv;
    private boolean isLogin;
    private UserBean.DataBean userInfo;
    private LikeTypeAdapter adapter;
    private PhotoView commodityIv;
    private Button sureBtn;
    List<GoodsBean.DataBean> dataBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_details);
        isLogin = isLogin();
        //photoView.enable();

        initView();
        setListener();
        dataBean = (DetailsBean.DataBean) getIntent().getSerializableExtra("goods");

        commodityIv.enable();

        setViewContent();

        dataBeanList = new ArrayList<>();

        adapter = new LikeTypeAdapter(this,dataBeanList);
        likegv.setAdapter(adapter);
        getNetWork(URLUtils.guessYouLike_url);

    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        GoodsBean goodsBean = new Gson().fromJson(result, GoodsBean.class);
        List<GoodsBean.DataBean> list = goodsBean.getData();

        dataBeanList.addAll(list);
        adapter.notifyDataSetChanged();

    }

    //设置view显示端的内容
    private void setViewContent() {
        titleTv.setText(dataBean.getName());
        priceTv.setText("￥ :"+dataBean.getPrice()+"元");
        countTv.setText("库存:"+dataBean.getCount());
        desTv.setText(dataBean.getInfo());
        String photo = dataBean.getPhoto();
        if (!TextUtils.isEmpty(photo)) {
            String photourl = URLUtils.PUBLIC_URL+photo;
            Picasso.with(this).load(photourl).into(commodityIv);
        }

    }

    private void setListener() {
        backIv.setOnClickListener(onClickListener);
        buyBtn.setOnClickListener(onClickListener);
        addBtn.setOnClickListener(onClickListener);
        likegv.setOnItemClickListener(itemClickListener);
    }



    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    };


    View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();

            switch (id){
                case R.id.item_buy_btn:
                    if (isLogin) {
                        String msg = "购买之前请先登录";
                        showBuyDialog(1,msg);
                    }
                    else {
                        Toast.makeText(GoodsDetailsActivity.this,"下单前前请先登录！",Toast.LENGTH_SHORT).show();
                    }

                    break;

                case R.id.item_add_btn:
                    String msg = "添加购物车前请先登录";

                    showBuyDialog(2, msg);

                    break;

                case R.id.item_back:
                    finish();
                    break;

                case R.id.dialog_buynum_btn:
                    Intent intent = new Intent(GoodsDetailsActivity.this, CartActivity.class);
                    startActivity(intent);


                    break;

            }


        }
    };

    private void showBuyDialog(int flag, String msg) {
        if (isLogin) {
            BuyNumDialog buyNumDialog = new BuyNumDialog(GoodsDetailsActivity.this);
            buyNumDialog.show();
            buyNumDialog.setDialogWidth();
            buyNumDialog.setOnEnsureListener(new BuyNumDialog.OnEnsureListener() {
                @Override
                public void onEnsure(int num) {

                if (flag == 1){//购买的点击按钮
                    //跳转到支付界面
                    //传商品id的购买数量



                }
                else if (flag == 2){//加入购物车
                    //跳转到购物车界面



                    Map<String,String>map = new HashMap<>();
                    map.put("userId",userInfo.getId()+"");
                    map.put("commodityId",dataBean.getId()+"");
                    map.put("count",num+"");
                    System.out.println("----------"+num);

                    postNetWork(URLUtils.insertShoppingCar_url,map);
                }
                buyNumDialog.cancel();

                }
            });
        }
        else {

            Toast.makeText(GoodsDetailsActivity.this, msg,Toast.LENGTH_SHORT).show();

        }
    }

    private boolean isLogin() {
        userInfo = SaveUserUtils.getUserInfo(this);
        if (userInfo == null){
            return false;
        }
        return true;
    }

    private void initView() {
         titleTv = findViewById(R.id.item_title);
         desTv = findViewById(R.id.item_des);
         countTv = findViewById(R.id.item_count);
         priceTv = findViewById(R.id.item_price);
         commodityIv = findViewById(R.id.item_commodity);
         addBtn = findViewById(R.id.item_add_btn);
         buyBtn = findViewById(R.id.item_buy_btn);
         backIv = findViewById(R.id.item_back);
         likegv = findViewById(R.id.details_gv);
         sureBtn = findViewById(R.id.dialog_buynum_btn);


    }


}